import { createStore } from 'vuex'

const store = createStore({
  state: {
    // 基本配置
    dataCount: 50000,
    generationMode: 'online', // 'online' 或 'offline'
    dataSource: '',
    storageSpace: '7天（免费用户）',
    fileFormat: 'CSV格式',
    
    // 字段配置
    fields: [
      {
        id: 1,
        name: '用户ID',
        dataType: 'BIGINT',
        length: '',
        constraint: 'PRIMARY KEY, NOT NULL',
        config: {}
      },
      {
        id: 2,
        name: '姓名',
        dataType: 'STRING',
        length: 32,
        constraint: 'NOT NULL',
        config: {}
      },
      {
        id: 3,
        name: '邮箱地址',
        dataType: 'STRING',
        length: 64,
        constraint: 'UNIQUE, NOT NULL',
        config: {}
      },
      {
        id: 4,
        name: '用户状态',
        dataType: 'ENUM',
        length: 10,
        constraint: "DEFAULT 'active'",
        config: {}
      },
      {
        id: 5,
        name: '注册时间',
        dataType: 'TIMESTAMP',
        length: '',
        constraint: 'NOT NULL',
        config: {}
      }
    ],
    
    // 用户信息
    user: {
      isLoggedIn: false,
      username: ''
    },
    
    // 历史记录
    history: []
  },
  
  mutations: {
    SET_DATA_COUNT(state, count) {
      state.dataCount = count
    },
    
    SET_GENERATION_MODE(state, mode) {
      state.generationMode = mode
    },
    
    SET_DATA_SOURCE(state, source) {
      state.dataSource = source
    },
    
    SET_STORAGE_SPACE(state, space) {
      state.storageSpace = space
    },
    
    SET_FILE_FORMAT(state, format) {
      state.fileFormat = format
    },
    
    ADD_FIELD(state, field) {
      state.fields.push({
        ...field,
        id: Date.now()
      })
    },
    
    UPDATE_FIELD(state, { index, field }) {
      state.fields[index] = { ...field }
    },
    
    REMOVE_FIELD(state, index) {
      state.fields.splice(index, 1)
    },
    
    SET_USER(state, user) {
      state.user = user
    },
    
    ADD_HISTORY(state, record) {
      state.history.unshift({
        ...record,
        id: Date.now(),
        timestamp: new Date().toISOString()
      })
    }
  },
  
  actions: {
    updateDataCount({ commit }, count) {
      commit('SET_DATA_COUNT', count)
    },
    
    updateGenerationMode({ commit }, mode) {
      commit('SET_GENERATION_MODE', mode)
    },
    
    updateDataSource({ commit }, source) {
      commit('SET_DATA_SOURCE', source)
    },
    
    updateStorageSpace({ commit }, space) {
      commit('SET_STORAGE_SPACE', space)
    },
    
    updateFileFormat({ commit }, format) {
      commit('SET_FILE_FORMAT', format)
    },
    
    addField({ commit }, field) {
      commit('ADD_FIELD', field)
    },
    
    updateField({ commit }, payload) {
      commit('UPDATE_FIELD', payload)
    },
    
    removeField({ commit }, index) {
      commit('REMOVE_FIELD', index)
    },
    
    login({ commit }, user) {
      commit('SET_USER', { ...user, isLoggedIn: true })
    },
    
    logout({ commit }) {
      commit('SET_USER', { isLoggedIn: false, username: '' })
    },
    
    addHistory({ commit }, record) {
      commit('ADD_HISTORY', record)
    }
  },
  
  getters: {
    isLoggedIn: state => state.user.isLoggedIn,
    username: state => state.user.username,
    fieldCount: state => state.fields.length,
    totalHistory: state => state.history.length
  }
})

export default store